﻿using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine;
using OnefallGames;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{

	public static UIManager Instance { private set; get; }



	//Gameplay UI
	[SerializeField] private GameObject gameplayUI;
	[SerializeField] private GameObject stateUI;
	[SerializeField] private Text stateTxt;
	[SerializeField] private GameObject coinUI;
	[SerializeField] private Text coinsTxt;
	[SerializeField] private GameObject bossFightUI;
	[SerializeField] private Text bossNameTxt;
	[SerializeField] private Transform knifeCountUI;
	[SerializeField] private Image gameplayPanel;

	//Revive UI
	[SerializeField] private GameObject reviveUI;
	[SerializeField] private Image reviveCoverImg;

	//GameOver UI
	[SerializeField] private GameObject gameOverUI;
	[SerializeField] private Text gameNameTxt;
	[SerializeField] private Text highestStateTxt;
	[SerializeField] private RawImage shareImage;
	[SerializeField] private GameObject freeCoinsBtn;
	[SerializeField] private Button dailyRewardBtn;
	[SerializeField] private Text dailyRewardText;
	[SerializeField] private GameObject playBtn;
	[SerializeField] private GameObject restartBtn;
	//[SerializeField] private GameObject shareBtn;
	[SerializeField] private GameObject soundOnBtn;
	[SerializeField] private GameObject soundOffBtn;
	[SerializeField] private GameObject musicOnBtn;
	[SerializeField] private GameObject musicOffBtn;

	//References
	[SerializeField] private AnimationClip servicesBtns_Show;
	[SerializeField] private AnimationClip servicesBtns_Hide;
	[SerializeField] private AnimationClip settingBtns_Hide;
	[SerializeField] private AnimationClip settingBtns_Show;
	[SerializeField] private Animator settingAnim;
	[SerializeField] private Animator servicesAnim;


	public bool IsWatchAdToContinue { private set; get; }

	private void OnEnable ()
	{
		GameManager.GameStateChanged += GameManager_GameStateChanged;
	}


	private void OnDisable ()
	{
		GameManager.GameStateChanged -= GameManager_GameStateChanged;
	}

	private void GameManager_GameStateChanged (GameState obj)
	{
		if (obj == GameState.GameOver) {
			StartCoroutine (ShowGameOverUI (0.5f));
		} else if (obj == GameState.Playing) {
			gameOverUI.SetActive (false);
			reviveUI.SetActive (false);           
			gameplayUI.SetActive (true);

			if (GameManager.Instance.IsBossState) {
				stateUI.SetActive (false);
				coinUI.SetActive (false);
				bossFightUI.SetActive (true);
			} else {
				bossFightUI.SetActive (false);
				coinUI.SetActive (true);
				stateUI.SetActive (true);
				stateTxt.text = "状态: " + StateManager.Instance.CurrentState.ToString ();
			}
			StartCoroutine (FadingOutGameplayPanel ());
		} else if (obj == GameState.Revive) {
			StartCoroutine (ShowReviveUI (0.5f));
		} else if (obj == GameState.FinishState) {
			StartCoroutine (FadingInGameplayPanel (1.5f));
		}
	}

	void Awake ()
	{
		if (Instance == null) {
			Instance = this;
		} else {
			DestroyImmediate (Instance.gameObject);
			Instance = this;
		}

	}

	void OnDestroy ()
	{
		if (Instance == this) {
			Instance = null;
		}
	}




	// Use this for initialization
	void Start ()
	{

		if (!GameManager.IsRestart) { //This is the first load
			UIPatterns (false);
		} else {
			gameOverUI.SetActive (false);
			reviveUI.SetActive (false);
			gameplayUI.SetActive (true);
		}
	}

	public void UIPatterns (bool state)
	{
		GameManager.IsRestart = state;
		gameplayUI.SetActive (false);
		reviveUI.SetActive (false);
		gameOverUI.SetActive (true);

		shareImage.gameObject.SetActive (false);
		highestStateTxt.gameObject.SetActive (false);
		freeCoinsBtn.SetActive (false);
		dailyRewardBtn.gameObject.SetActive (false);
		restartBtn.SetActive (false);
		//shareBtn.SetActive (false);
		playBtn.SetActive (true);

	}

	// Update is called once per frame
	void Update ()
	{

		UpdateMusicButtons ();
		UpdateMuteButtons ();

		coinsTxt.text = CoinManager.Instance.Coins.ToString ();

		if (DailyRewardManager.Instance.CanRewardNow ()) {
			dailyRewardText.text = "可领取";
			dailyRewardBtn.interactable = true;
		} else {
			string hours = DailyRewardManager.Instance.TimeUntilNextReward.Hours.ToString ();
			string minutes = DailyRewardManager.Instance.TimeUntilNextReward.Minutes.ToString ();
			string seconds = DailyRewardManager.Instance.TimeUntilNextReward.Seconds.ToString ();
			dailyRewardText.text = hours + ":" + minutes + ":" + seconds;
			dailyRewardBtn.interactable = false;
		}

	}


	////////////////////////////Publish functions
	public void PlayButtonSound ()
	{
		SoundManager.Instance.PlaySound (SoundManager.Instance.button);
	}



	public void PlayBtn ()
	{
		if (CoinManager.Instance.Coins >= 20) {
			CoinManager.Instance.RemoveCoins (20);
			GameManager.Instance.PlayingGame ();
		} else
			FindObjectOfType<GameManager> ()._noCoinsDia.SetActive (true);
	}

	public void RestartBtn (float delay)
	{
		GameManager.Instance.LoadScene (SceneManager.GetActiveScene ().name, delay);
	}

	public void NativeShareBtn ()
	{
		ShareManager.Instance.NativeShare ();
	}

	public void CharacterBtn ()
	{
		GameManager.Instance.LoadScene ("Character", 0.5f);
	}

	public void SettingBtn ()
	{
		servicesAnim.Play (servicesBtns_Hide.name);
		settingAnim.Play (settingBtns_Show.name);
	}

	public void ToggleSound ()
	{
		SoundManager.Instance.ToggleMute ();
	}

	public void ToggleMusic ()
	{
		SoundManager.Instance.ToggleMusic ();
	}

	public void RateAppBtn ()
	{
		Application.OpenURL (ShareManager.Instance.AppUrl);
	}

	public void FacebookShareBtn ()
	{
		ShareManager.Instance.FacebookShare ();
	}

	public void BackBtn ()
	{
		settingAnim.Play (settingBtns_Hide.name);
		servicesAnim.Play (servicesBtns_Show.name);
	}


	public void DailyRewardBtn ()
	{
		DailyRewardManager.Instance.ResetNextRewardTime ();
		GameManager.Instance.CreateCoinsForReward (DailyRewardManager.Instance.GetRandomReward (), 0.5f);
	}


	public void FreeCoinsBtn ()
	{
		freeCoinsBtn.SetActive (false);
//		AdManager.Instance.ShowRewardedVideoAd ();
		IsWatchAdToContinue = false;
	}

	public void ReviveBtn ()
	{
		reviveUI.SetActive (false);
//		AdManager.Instance.ShowRewardedVideoAd ();
		IsWatchAdToContinue = true;
	}

	public void SkipBtn ()
	{
		reviveUI.SetActive (false);
		GameManager.Instance.GameOver ();
	}

	public void ExitGame ()
	{
		Application.Quit ();
	}

	public void MoreGames ()
	{
		Application.OpenURL ("https://play.google.com/store/apps/developer?id=Mohammad+Faisal+Khan");
	}

	public void PrivacyPolicy ()
	{
		Application.OpenURL ("https://mohammadfaisalkhan2402.blogspot.com/p/mohammad-faisal-khan.html?m=1");
	}

	/////////////////////////////Private functions
	void UpdateMuteButtons ()
	{
		if (SoundManager.Instance.IsMuted ()) {
			soundOnBtn.gameObject.SetActive (false);
			soundOffBtn.gameObject.SetActive (true);
		} else {
			soundOnBtn.gameObject.SetActive (true);
			soundOffBtn.gameObject.SetActive (false);
		}
	}


	void UpdateMusicButtons ()
	{
		if (SoundManager.Instance.IsMusicOff ()) {
			musicOffBtn.gameObject.SetActive (true);
			musicOnBtn.gameObject.SetActive (false);
		} else {
			musicOffBtn.gameObject.SetActive (false);
			musicOnBtn.gameObject.SetActive (true);
		}
	}


	private IEnumerator ShowGameOverUI (float delay)
	{
		yield return new WaitForSeconds (delay);

		gameplayUI.SetActive (false);
		gameOverUI.SetActive (true);

		gameNameTxt.gameObject.SetActive (false);
		highestStateTxt.gameObject.SetActive (true);
		highestStateTxt.text = "最高等级: " + StateManager.Instance.HighestState.ToString ();
		playBtn.SetActive (false);
		restartBtn.SetActive (true);
		freeCoinsBtn.SetActive (AdManager.Instance.IsRewardedVideoAdReady ());
		dailyRewardBtn.gameObject.SetActive (true);
		//shareBtn.SetActive (true);
		shareImage.gameObject.SetActive (true);
		shareImage.texture = GameManager.Instance.LoadedScrenshot ();
	}

	private IEnumerator ShowReviveUI (float delay)
	{
		yield return new WaitForSeconds (delay);

		reviveUI.SetActive (true);
		StartCoroutine (ReviveCountDown ());
	}

	IEnumerator ReviveCountDown ()
	{
		float t = 0;
		while (t < GameManager.Instance.reviveWaitTime) {
			if (!reviveUI.activeInHierarchy)
				yield break;
			t += Time.deltaTime;
			float factor = t / GameManager.Instance.reviveWaitTime;
			reviveCoverImg.fillAmount = Mathf.Lerp (1, 0, factor);
			yield return null;
		}
		reviveUI.SetActive (false);
		GameManager.Instance.GameOver ();
	}

	private IEnumerator FadingOutGameplayPanel ()
	{
		gameplayPanel.gameObject.SetActive (true);

		float t = 0;
		float fadingTime = 1f;
		Color startColor = gameplayPanel.color;
		Color endColor = new Color (startColor.r, startColor.g, startColor.b, 0);
		while (t < fadingTime) {
			t += Time.deltaTime;
			float factor = t / fadingTime;
			gameplayPanel.color = Color.Lerp (startColor, endColor, factor);
			yield return null;
		}

		gameplayPanel.gameObject.SetActive (false);
	}

	private IEnumerator FadingInGameplayPanel (float delay)
	{
		yield return new WaitForSeconds (delay);

		gameplayPanel.gameObject.SetActive (true);

		float t = 0;
		float fadingTime = 1f;
		Color startColor = gameplayPanel.color;
		Color endColor = new Color (startColor.r, startColor.g, startColor.b, 1);
		while (t < fadingTime) {
			t += Time.deltaTime;
			float factor = t / fadingTime;
			gameplayPanel.color = Color.Lerp (startColor, endColor, factor);
			yield return null;
		}

		GameManager.Instance.LoadScene (SceneManager.GetActiveScene ().name, 0f);
	}


	/// <summary>
	/// Disable GameOverUI for reward coins
	/// </summary>
	public void DisableGameOverUI ()
	{
		StartCoroutine (DisableAndEnableGameOverUI ());
	}

	private IEnumerator DisableAndEnableGameOverUI ()
	{
		gameOverUI.SetActive (false);
		yield return new WaitForSeconds (3f);
		gameOverUI.SetActive (true);
	}

	/// <summary>
	/// Update knife images base on knifeCount
	/// </summary>
	/// <param name="knifeCount"></param>
	public void UpdateKnifesImg (int knifeCount, Color disableColor, Color enableColor)
	{
		int knifeLost = knifeCountUI.childCount - knifeCount;
		for (int i = 0; i < knifeLost; i++) {
			knifeCountUI.GetChild (i).GetComponent<Image> ().color = disableColor;
		}

		for (int i = knifeLost; i < knifeCountUI.childCount; i++) {
			knifeCountUI.GetChild (i).GetComponent<Image> ().color = enableColor;
		}
	}

	/// <summary>
	/// Set boss's name
	/// </summary>
	/// <param name="name"></param>
	public void SetBossName (string name)
	{
		bossNameTxt.text = name.ToUpper ();
	}

    public void TouchWatchAds()
    {
        GameManager.Instance.CreateCoinsForReward(DailyRewardManager.Instance.GetRandomReward(), 0.5f);
        string videoAdid = VideData.Instance.GetVide();
        TTSDK.TTRewardedVideoAd r = AdMgr.Instance.ShowVideoAdNew(videoAdid);

        //创建激励视频关闭回调
        r.OnClose += (bol, val) =>
        {
            Debug.Log("关闭广告 ，是否已经看完" + bol);
            //看完广告获得提示
            if (bol)
            {
                GameManager.Instance.CreateCoinsForReward(DailyRewardManager.Instance.GetRandomReward(), 0.5f);
                MaidianNew.MaidianBackCallback(" ");
                Debug.Log("广告看完");
            }
            else
            {
                Debug.Log("广告未看完");
            }
        };

        //创建报错回调  
        r.OnError += (code, errorMessage) =>
        {

            Debug.Log("code:" + code + "错误信息：" + errorMessage);
        };

        //展示广告（新版本必须调用，不然无法展示出来广告）
        r.Show();

    }
}
